package www.wmx.com.xssc.util.similarity;

import com.hankcs.hanlp.seg.common.Term;
import java.util.*;

public class ShinglingComparator {

    private static final int SHINGLE_SIZE = 3; // 定义Shingle的大小

    // 提取给定文本列表的Shingles
    private static Set<List<String>> extractShingles(List<Term> text) {
        Set<List<String>> shingles = new HashSet<>();
        for (int i = 0; i <= text.size() - SHINGLE_SIZE; i++) {
            List<String> shingle = new ArrayList<>();
            for (int j = 0; j < SHINGLE_SIZE; j++) {
                shingle.add(text.get(i + j).word); // 假设Term类有一个名为word的字段存储单词文本
            }
            shingles.add(shingle);
        }
        return shingles;
    }

    public  static  double getJaccardSimilarity(String s1,String s2){
        List<Term> termList1 = HanLPPreprocessor.segment(s1);
        List<Term> termList2 = HanLPPreprocessor.segment(s2);
        double similarity = calculateSimilarity(termList1, termList2);
        return  similarity;
    }

    // 计算两个文本列表之间的Jaccard相似度
    public static double calculateSimilarity(List<Term> text1, List<Term> text2) {
        Set<List<String>> shingles1 = extractShingles(text1);
        Set<List<String>> shingles2 = extractShingles(text2);

        // 计算交集大小
        Set<List<String>> intersection = new HashSet<>(shingles1);
        intersection.retainAll(shingles2);
        int commonShingles = intersection.size();

        // 计算并集大小
        Set<List<String>> union = new HashSet<>(shingles1);
        union.addAll(shingles2);
        int totalShingles = union.size();

        // 避免除以零的错误
        if (totalShingles == 0) {
            return 0.0;
        }

        // 计算Jaccard相似度
        return (double) commonShingles / totalShingles;
    }
}
